<?php
/************************************
*   熊店
*   @file:/server/bearshop/page/p_vcode.php
*   @intro:
*   @author:new_shop
*   @email:new_shop@163.com    
*   @date:Thu Feb 11 18:58:24 CST 2010
************************************/

class p_vcode extends page_controllor   
{
    private $_im;
    private $_img_width = 100;
    private $_img_height = 20;
    private $_vcode;
    private $_fontcolor;
    private $_ptcolor;
    private $_colornum = 9;
    
    public function __construct($get_param)
    {
        $this->__need_login = false;
        parent::__construct($get_param);
    }
    public function run()
    {
        
        //初始化验证码
        
        $oSess = K::session();
        $this->_vcode = strval($oSess->init_validate());
        
        
        
        //创建图片
        $this->_im = @imagecreate ($this->_img_width, $this->_img_height);
        //背景色
        $bg_color = mt_rand(140,210);
        $background_color = imagecolorallocate ($this->_im, $bg_color, $bg_color, $bg_color);
        $background_color_2 = imagecolorallocate ($this->_im, 240, 240, 240);
	
        //字体颜色
        $this->_fontcolor = array();
        for ($i=0;$i<=$this->_colornum;$i++)
        {
            $ft_color = mt_rand(40,80);
            $this->_fontcolor[] = imagecolorallocate ($this->_im, $ft_color, $ft_color, $ft_color);
        }
        //干扰色
        $this->_ptcolor = array();
        for ($i=0;$i<=$this->_colornum;$i++)
        {
            $pt_color = mt_rand(120,180);
            $this->_ptcolor[] = imagecolorallocate ($this->_im, $pt_color, $pt_color, $pt_color);
        }
        
        //粗横线
        for ($i=0;$i<3;$i++)
        {
            $direct = mt_rand(0,1);
            $this->_write_a_line(   ($direct==0 ? 0 : $this->_img_width)
                            ,mt_rand(-1 , $this->_img_height/2+2)
                            ,($direct<>0 ? 0 : $this->_img_width)
                            ,mt_rand($this->_img_height/2 , $this->_img_height+1) 
                            ,$this->_ptcolor
                            , mt_rand(5,9));
        }
        
        // 字体
        $font = K5_DATA_PATH.'/bookos.ttf';
        for ($i = 0 ; $i < strlen($this->_vcode) ; $i++)
        {
            $num = $this->_vcode{$i};       //
            
            
            $size = rand(16,18);            //字体大小
            $angle = rand(-20, 20);			// 角度
            $lastpost = imagettfbbox($size, $angle, $font, $num);
            
            $nColor = mt_rand(0 , $this->_colornum);
            
            $x = 5+$i*19;
            $y = mt_rand(14,16);
            
            imagettftext($this->_im, $size, $angle, $x+1 , $y+2, $background_color_2, $font, $num);
            imagettftext($this->_im, $size, $angle, $x , $y, $this->_fontcolor[$nColor], $font, $num);
            
            $x += $inteval;
        }
        
        //粗横线
        /*
        for ($i=0;$i<3;$i++)
        {
            $direct = mt_rand(0,1);
            $this->_write_a_line(   ($direct==0 ? 0 : $this->_img_width)
                            ,mt_rand(-1 , $this->_img_height/2+2)
                            ,($direct<>0 ? 0 : $this->_img_width)
                            ,mt_rand($this->_img_height/2 , $this->_img_height+1)
                            ,$this->_fontcolor
                            , 1);
        }
        */

        imagesetthickness($this->_im , 1);
        ImageRectangle($this->_im, 0, 0, $this->_img_width-1, $this->_img_height-1, $this->_fontcolor[0]);

        header ("Content-type: image/png");
        imagepng($this->_im);
        imagedestroy($this->_im);
        die;
    }
    
    private function _write_a_line($x, $y, $x1, $y1, $aColor , $thick=3)
    {
    	imagesetthickness ($this->_im, $thick);

    	$line_color = $aColor[mt_rand(0,$this->_colornum)];
    		
    	imageline  ($this->_im, $x, $y, $x1, $y1, $line_color);
    }
    
}
?>